import { Body, Controller, Get, Post, Query, UseFilters } from "@nestjs/common";
import { RoleService } from "./role.service";
import { Role } from "./entities/role.entity";
import { HttpExceptionFilter } from "src/exception/http-exception.filter";
import { wrapperResponse } from "src/utils";

@Controller('/role') // 设置路由前缀为 /roles
export class RoleController {
    constructor(
        private readonly roleService:RoleService
    ){}

    @Get('/detail')
    @UseFilters(new HttpExceptionFilter)
    getDetail(@Query() { id }):Promise<Role | null> {
        return wrapperResponse(
            this.roleService.findOne(id),
            '获取角色详情成功'
        )
    }

    @Get('/list')
    @UseFilters(new HttpExceptionFilter)
    getAllRole(@Query() query):Promise<Role[]> {
        return wrapperResponse(
            this.roleService.findAll(query),
            '查询角色成功'
        )
    }
    @Post('/create')
    @UseFilters(new HttpExceptionFilter)
    createRole(@Body() body) {
        return wrapperResponse(
            this.roleService.create(body),
            '创建角色成功'
        )
    }

    @Post('/update')
    @UseFilters(new HttpExceptionFilter)
    update(@Body() body) {
        return wrapperResponse(
            this.roleService.update(body),
            '更新角色成功'
        )
    }
    @Post('/del')
    @UseFilters(new HttpExceptionFilter)
    delete(@Body() body:{id:string}) {
        return wrapperResponse(
            this.roleService.delete(body.id),
            '删除角色成功'
        )
    }

    // 根据角色ID查询菜单
    @Get('/getRoleMenuList')
    @UseFilters(new HttpExceptionFilter)
    getRoleMenuList(@Query() { roleId }):Promise<Role | null> {
        return wrapperResponse(
            this.roleService.findRoleMenu(roleId),
            '获取角色菜单成功'
        )
    }
    // 根据角色ID查询权限
    @Get('/getRolePermissionList')
    @UseFilters(new HttpExceptionFilter)
    getRolePermission(@Query() { roleId }):Promise<Role | null> {
        return wrapperResponse(
            this.roleService.findRolePermission(roleId),
            '获取角色权限成功'
        )
    }
    // 根据角色ID绑定菜单和权限
    @Post('/bindRoleMenuAndPermission')
    @UseFilters(new HttpExceptionFilter)
    bindRoleMenuAndPermission(@Body() body) {
        return wrapperResponse(
            this.roleService.bindRoleMenuAndPermission(body),
            '新建角色和菜单绑定关系成功'
        )
    }
}